package com.linln.modules.member.service.impl;

import com.linln.modules.member.repository.MemberAgentMoneyChangeRepository;
import com.linln.modules.member.repository.MemberAgentMoneyRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.linln.common.data.PageSort;
import com.linln.modules.member.domain.MemberAgent;
import com.linln.modules.member.domain.MemberAgentMoney;
import com.linln.modules.member.repository.MemberAgentMoneyRepository;
import com.linln.modules.member.service.MemberAgentMoneyService;
import com.linln.modules.member.service.MemberAgentService;

/**
 * @author hct
 * @date 2019/09/12
 */
@Service
public class MemberAgentMoneyServiceImpl implements MemberAgentMoneyService {

    @Autowired
    private MemberAgentMoneyRepository memberAgentMoneyRepository;

    @Autowired
    private MemberAgentMoneyChangeRepository memberAgentMoneyChangeRepository;

    @Autowired
    private MemberAgentService memberAgentRepository;

    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    @Transactional
    public MemberAgentMoney getById(Long id) {
        return memberAgentMoneyRepository.findById(id).orElse(null);
    }
   
   @Override
    public   Boolean delCollect(Long id){
	   boolean isOk=false;
	   try{
		   memberAgentMoneyRepository.deleteById(id);
        isOk=true;
	   }catch(Exception e){
		   
	   }
       return isOk;
   }
    
    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<MemberAgentMoney> getPageList(Example<MemberAgentMoney> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return memberAgentMoneyRepository.findAll(example, page);
    }

    /**
     * 提现使用
     * @param membermoney 实体对象
     */
    @Override
    @Transactional
    public MemberAgentMoney saveTixian(MemberAgentMoney membermoney) {
    	MemberAgent agent=membermoney.getMemberagent();
    	memberAgentRepository.updateCurmoney(agent.getCurmoney()-membermoney.getCurmoney(),agent.getId());
    	//提现前的钱
        membermoney.setPremoney(agent.getCurmoney());
        //提现后的钱
        membermoney.setAftermoney(agent.getCurmoney()-membermoney.getCurmoney());
    	return memberAgentMoneyRepository.save(membermoney);
    }

    /**
     * 记录代理商提现信息
     * @param membermoney 实体对象
     */
    @Override
    @Transactional
    public MemberAgentMoney save(MemberAgentMoney membermoney) {
    	return memberAgentMoneyRepository.save(membermoney);
    }
     
}